let query_item_name = '';
let query_item_state = '';
// 当前的第几页
let curr = 1;
let size = Number($('.select_page').val());
let pagenum = 0;
// 生成分页器
function createPage(curr,total,size){
	pagenum = Math.ceil((total/size));
	let html =''
	for(let i=1; i<=pagenum; i++){
		if(curr == i){
			html += `
					<li class="active">${i}</li>
					`
		}else{
			html += `
					<li>${i}</li>
					`
		}
	}
	$('.page-item').empty().append(html);
}
function getItemList(){
	$.ajax({
		url:'http://chonghekj.com/crud/index.php/Home/Index/getItemList',
		type:'GET',
		dataType:'json',
		data:{
			pageNumber : curr,
			pageSize : size,
			item_name : query_item_name,
			item_state : query_item_state
		},
		success : function(res){
			let html = '';
			for(let i=0; i<res.rows.length; i++){
				html += `
						<tr>
							<td>
								<input type = 'checkbox' class='check_item'>
							</td>
							<td class="item_id">${res.rows[i].item_id}</td>
							<td class="item_name">${res.rows[i].item_name}</td>
							<td class="item_price">${res.rows[i].item_price}</td>
							<td class="item_format">${res.rows[i].item_format}</td>
							<td class ='item_total'>${res.rows[i].item_total}</td>
							<td>${res.rows[i].item_state}</td>
							<td class="item_inventory">${res.rows[i].item_inventory}</td>
							<td>${res.rows[i].create_time}</td>
							<td>${res.rows[i].update_time}</td>
						</tr>
						`
			}
			$('tbody').empty().append(html);
			createPage(curr,res.total,size);
		}
	})
}
getItemList()

// 添加数据
$('.add_form').click(function(){
	$('.add_box').css('display','block');
})
$('.add_item_btn').click(function(){
	let item_name = $('.add_item_name').val();
	let item_price = $('.add_item_price').val();
	let item_format = $('.add_item_format').val();
	let item_total = $('.add_item_totle').val();
	let item_inventory = $('.add_item_inventory').val();
	
	$.ajax({
		url:'http://chonghekj.com/crud/index.php/Home/Index/addItem',
		type:'post',
		dataType:'json',
		data:{
			item_name,
			item_price,
			item_format,
			item_total,
			item_inventory,
		},
		success:function(res){
			$('.add_item_name').val("");
			$('.add_item_price').val("");
			$('.add_item_format').val("");
			$('.add_item_totle').val("");
			$('.item_inventory').val("");
			$('.add_box').css('display','none');
			getItemList()
		}
	})
	
})
// 修改数据
$('.update_form').click(function(){
	$('.update_box').css('display','block');
})
$('tbody').on('click','.check_item',function(){
	let item = $('.check_item:checked');
	let item_id = item.parent().parent().find('.item_id').text();
	let item_name = item.parent().parent().find('.item_name').text();
	let item_price = item.parent().parent().find('.item_prive').text();
	let item_format = item.parent().parent().find('.item_format').text();
	let item_total = item.parent().parent().find('.item_totle').text();
	let item_inventory = item.parent().parent().find('.item_inventory').text();
	
	$('.update_item_id').val(item_id);
	$('.update_item_name').val(item_name);
	$('.update_item_price').val(item_price);
	$('.update_item_format').val(item_format);
	$('.update_item_total').val(item_total);
	$('.update_inventory').val(item_inventory);
	
	$('.update_item_btn').click(function(){
		item_id = $('.update_item_id').val();
		item_name = $('.update_item_name').val();
		item_price = $('.update_item_price').val();
		item_format = $('.update_item_format').val();
		item_total = $('.update_item_total').val();
		item_inventory = $('.update_inventory').val();
		$('.update_box').css('display','none');
		$.ajax({
			url:'http://chonghekj.com/crud/index.php/Home/Index/editItem',
			type:'post',
			dataType:'json',
			data:{
				item_id,
				item_name,
				item_price,
				item_format,
				item_total,
				item_inventory
			},
			success:function(){
				getItemList();
			}
		})
	})
	
})
// 删除数据
$('.delete_form').click(function(){
	let items = $('.check_item:checked');
	let ids= [];
	for(let i = 0; i<items.length; i++){
		ids.push($(items[i]).parent().parent().find('.item_id').text())
	}
	$.ajax({
		url:'http://chonghekj.com/crud/index.php/Home/Index/deleteItem',
		type:'post',
		dataType:'json',
		data:{
			ids
		},
		success:function(res){
			getItemList();
			ids = [];
		}
	})
})
// 查询
$('.query').click(function(){
	query_item_name = $('.item_name').val();
	query_item_state = $('.item_state').val();
	// $('.item_name').val('');
	getItemList();
})
$('.select_page').change(function(){
	size = Number($('.select_page').val());
	getItemList();
})

$('.page-item').on('click','li', function(){
	curr = Number($(this).text());
	getItemList();
})
$('.prev').click(function(){
	if(curr > 1){
		curr = curr - 1
		getItemList();
	}
})
$('.next').click(function(){
	if(curr < pagenum){
		curr = curr + 1
		getItemList();
	}
})
